
import common.commonFunction;

import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.validators.Validator;

import programMaster.BranchWithSpecialization;
import programMaster.ProgramMaster;

[Embed(source="images/error.png")]private var errorIcon:Class;
[Embed(source="images/success.png")]private var successIcon:Class;
[Embed(source="images/reset.png")]private var resetIcon:Class;
[Embed(source="images/question.png")]private var questionIcon:Class;

var pm:ProgramMaster=new ProgramMaster();
var bws:BranchWithSpecialization=new BranchWithSpecialization();

public function changeProgram():void
{
	programCodeText.text="";
	programCodeText.errorString="";
	programCodeText.text=pm.codeXML.branch.(branchName==programNameComboBox.selectedLabel).branchCode;


}

public function showDetails():void
{
		if(!(programNameComboBox.selectedIndex==-1))
	{
				programNameComboBox.errorString="";	
	     branchSpecializationPanel.visible=true;
	    httpBranchSpecList();
	}
	else
	{
		programNameComboBox.errorString=resourceManager.getString('Messages','noRecordSelected');
		Alert.show(resourceManager.getString('Messages','pleasecheckEntriesinRed'),resourceManager.getString('Messages','error'),0,null,null,errorIcon);
    }

	   

}


public function deleteDetails():void
{
	var selectedValues:ArrayCollection=	commonFunction.getSelectedRowData(branchSpecializationGrid);
	var progId:String="";
	var branchId:String="";
	var specId:String="";
	for each(var obj:Object in  selectedValues)
	{
		progId+=obj.programId+"|";
		branchId+=obj.branchCode+"|";
		specId+=obj.specCode+"|";
	}
	var params:Object=new Object();
	params["progId"]=progId;
	params["branchId"]=branchId;
	params["specId"]=specId;
	deleteBranch(params);
}

public function setSpecialization():void
			{
				var noneStatus:String=commonFunction.getConstants('none');
				if(specializationCombo.selectedItem.branchCode==noneStatus)
				{
					ActiveSemCombo.selectedIndex=-1;
					ActiveSemCombo.enabled=false;
				}
				else
				{
					ActiveSemCombo.enabled=true;
					ActiveSemCombo.selectedIndex=0;
				}
			}


public function addBranch():void
{
	if(Validator.validateAll([branchValidator,specializationValidator]).length==0)
	{
		if( !(((branchCombo.selectedIndex<0)||(specializationCombo.selectedIndex<0))
	    		||((branchCombo.selectedItem.branchCode==commonFunction.getConstants('none')) 
	    		&& (specializationCombo.selectedItem.branchCode==commonFunction.getConstants('none')) )) )
		{
			var c:int=0;
		
		for each (var obj:Object in bsAC){
			
			if(obj.branchCode.toString() == branchCombo.selectedItem.branchCode.toString() && obj.specCode.toString()==specializationCombo.selectedItem.branchCode.toString()){
				c++;
			}
		}
		
		
		
		if(c==0){
		var inputObj:Object=new Object();
		inputObj["userId"]=pm.userId;
		inputObj["programId"]=pm.codeXML.branch.(branchName==programNameComboBox.selectedLabel).programId;
		inputObj["programCode"]=programCodeText.text;
		inputObj["branchCode"]=branchCombo.selectedItem.branchCode;
		inputObj["specCode"]=specializationCombo.selectedItem.branchCode;
		if(specializationCombo.selectedItem.branchCode==commonFunction.getConstants('none')){
		inputObj["activeSem"]=";";
		}else{
		inputObj["activeSem"]=ActiveSemCombo.selectedItem.branchCode;	
		}
	//	Alert.show("not duplicate"+c);
		addNewBranch(inputObj);
		}else{
			
		Alert.show(resourceManager.getString('Messages','duplicateEntry'),resourceManager.getString('Messages','error'),0,null,null,errorIcon);
			
		}
		
		
		}
		else
		{
			Alert.show(resourceManager.getString('Messages','invalidEntry'),resourceManager.getString('Messages','error'),0,null,null,errorIcon);
        }
	}
	else
	{
		Alert.show(resourceManager.getString('Messages','pleasecheckEntriesinRed'),resourceManager.getString('Messages','error'),0,null,null,errorIcon);
	}
}

public function addStartDate():void
{
	Alert.show(resourceManager.getString('Messages','successOnInsert'),resourceManager.getString('Messages','success'),0,null,null,successIcon);
}

private function loadCombo():void{
	pm.httpProgramCode.url=commonFunction.getConstants('url')+ "/programmaster/methodprogList.htm";
	pm.httpProgramCodeList();
	pm.codeListCb=programNameComboBox;
	httpBranch();
	
}


[Bindable]
public var branchSpecXml:XML;
public var programIdForBranchList:String;

/**
 * Function for getting program code list
 */
public function httpBranchSpecList():void{
		var params:Object=new Object();
	params["userId"]=pm.userId;
//	if(programNameComboBox.selectedIndex>0){
	params["programId"]=pm.codeXML.branch.(branchName==programNameComboBox.selectedLabel).programId;
//	}
//	if(programCodeText.text != ""){
//		params["criteria"]="code";
//	params["programCode"]=programCodeText.text
//	}else{
		params["criteria"]="not code";
//	}
		params["time"]=new Date().getTime();
	httpBranchSpec.send(params);
}
private function faultHandler_BranchSpec(event:FaultEvent):void{
         mx.controls.Alert.show(event.fault.message,resourceManager.getString('Messages','error'));
    }
    
     var bsAC:ArrayCollection;
    
private function resultHandler_BranchSpec(event:ResultEvent):void{
	
   branchSpecXml=event.result as XML;  
   if(branchSpecXml.sessionConfirm == true)
             {
            Alert.show(resourceManager.getString('Messages','sessionInactive'));
          this.parentDocument.vStack.selectedIndex=0;
          this.parentDocument.loaderCanvas.removeAllChildren();
            }  
 bsAC=new ArrayCollection();
	var flag:Boolean=true;
   	for each (var object:Object in branchSpecXml.bs){
   	bsAC.addItem({programId:object.programId,programCode:object.programCode,programName:object.programName,
   	branchName:object.branchName,specName:object.specName,activeSemester:object.activeSemester,
   	branchCode:object.branchCode,specCode:object.specCode});
   	programIdForBranchList=object.programId;
   	}

branchSpecializationGrid.dataProvider=bsAC;

   }

[Bindable]
var branchXML:XML;
[Bindable]
var specXML:XML;
[Bindable]
var semXML:XML;

/**
 * Function for getting branch list
 */
public function httpBranch():void{
	var params:Object=new Object();
	params["userId"]=pm.userId;
	params["time"]=new Date().getTime();
	httpBranchList.send(params);
}
private function faultHandler_Branch(event:FaultEvent):void{
         mx.controls.Alert.show(event.fault.message,resourceManager.getString('Messages','error'));
    }
private function resultHandler_Branch(event:ResultEvent):void{
	  
   branchXML=event.result as XML;
if(branchXML.sessionConfirm == true)
             {
            Alert.show(resourceManager.getString('Messages','sessionInactive'));
          this.parentDocument.vStack.selectedIndex=0;
          this.parentDocument.loaderCanvas.removeAllChildren();
            }  
  
  branchCombo.dataProvider=branchXML.branch;
  
  
httpSpec();

   }
   
   
/**
 * Function for getting specialization list
 */
 private function httpSpec():void{
	var params:Object=new Object();
	params["userId"]=pm.userId;
	params["time"]=new Date().getTime();
	httpSpecList.send(params);
}
private function faultHandler_Spec(event:FaultEvent):void{
         mx.controls.Alert.show(event.fault.message,resourceManager.getString('Messages','error'));
    }
private function resultHandler_Spec(event:ResultEvent):void{
	
   specXML=event.result as XML;

 if(specXML.sessionConfirm == true)
             {
            Alert.show(resourceManager.getString('Messages','sessionInactive'));
          this.parentDocument.vStack.selectedIndex=0;
          this.parentDocument.loaderCanvas.removeAllChildren();
            }  
  specializationCombo.dataProvider=specXML.branch;
  
httpSemester();
   }
   
   
   
   /**
 * Function for getting semester list
 */
 private function httpSemester():void{
	var params:Object=new Object();
	params["userId"]=pm.userId;
		params["time"]=new Date().getTime();
	httpSemList.send(params);
}
private function faultHandler_Semester(event:FaultEvent):void{
         mx.controls.Alert.show(event.fault.message,resourceManager.getString('Messages','error'));
    }
private function resultHandler_Semester(event:ResultEvent):void{
	  
   semXML=event.result as XML;
  if(semXML.sessionConfirm == true)
             {
            Alert.show(resourceManager.getString('Messages','sessionInactive'));
          this.parentDocument.vStack.selectedIndex=0;
          this.parentDocument.loaderCanvas.removeAllChildren();
            }  
ActiveSemCombo.dataProvider=semXML.branch;
}

private function addNewBranch(params:Object):void{
		params["time"]=new Date().getTime();
	httpAddNewBranch.send(params);
}
private function faultHandler_NewBranch(event:FaultEvent):void{
         mx.controls.Alert.show(event.fault.message,resourceManager.getString('Messages','error'));
    }
private function resultHandler_NewBranch(event:ResultEvent):void{
	
	var resultXML:XML=event.result as XML;
	if(resultXML.sessionConfirm == true)
             {
            Alert.show(resourceManager.getString('Messages','sessionInactive'));
          this.parentDocument.vStack.selectedIndex=0;
          this.parentDocument.loaderCanvas.removeAllChildren();
            }  
	if(resultXML.exception.exceptionstring=="fail"){
		 mx.controls.Alert.show(resourceManager.getString('Messages','error'),resourceManager.getString('Messages','error'));
	}else{
		 Alert.show(resourceManager.getString('Messages','success'),resourceManager.getString('Messages','success'),0,null,null,successIcon);
	
	showDetails();
	}
  
}

/*
* function to delete branch specialization details
*/
 private function deleteBranch(params:Object):void{
 		params["time"]=new Date().getTime();
 	httpDeleteBranch.send(params);
 }
private function faultHandler_DeleteBranch(event:FaultEvent):void{
         mx.controls.Alert.show(event.fault.message,resourceManager.getString('Messages','error'));
    }
private function resultHandler_DeleteBranch(event:ResultEvent):void{
	var resultXml:XML=event.result as XML;
	if(resultXml.sessionConfirm == true)
             {
            Alert.show(resourceManager.getString('Messages','sessionInactive'));
          this.parentDocument.vStack.selectedIndex=0;
          this.parentDocument.loaderCanvas.removeAllChildren();
            } 
            if(resultXml.exception.exceptionstring=="ie"){
		Alert.show(resourceManager.getString('Messages','dependencyDeleteError'),resourceManager.getString('Messages','error'),0,null,null,errorIcon);
	}else{
            
	   Alert.show(resourceManager.getString('Messages','success'),resourceManager.getString('Messages','success'),0,null,null,successIcon);  
showDetails();
	}
            
 
}

